{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "717b3283",
   "metadata": {},
   "source": [
    "* 2023-10-23\n",
    "* week07"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bf39153",
   "metadata": {},
   "source": [
    "# 1.列表\n",
    "* 思考：如果把我们班同学的名字都存起来，你会怎么做？用变量name1、name2、name3...来存吗？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b2105f3",
   "metadata": {},
   "source": [
    "## 1.1体验列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bf7490a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"张三\"\n",
    "name2 = \"李华\"\n",
    "name3 = \"李鹏\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e85b7f9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5b03f065",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8e6f0260",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李鹏'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4320eb5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "name =\"张三，李华，李鹏，李小明\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "90e7cbb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三，李华，李鹏，李小明'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "97600805",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0918af65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b1a431a",
   "metadata": {},
   "source": [
    "* 综上，一种新的数据结构就此而生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f4e1803f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0868195a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2e47453a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fde3dc20",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_学号 = [1,2,3] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9399da8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ee27cdec",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树人\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0c7a83dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树人', '李鹏', '李小明']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5803e13d",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[0] = \"黄俊英\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5083cdd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李树人', '李鹏', '李小明']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6dce7f15",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_黄俊英 = [\"黄俊英\",1,19,180,100,\"Bobby\",[\"python\",\"移动互联网\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "66661628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', 1, 19, 180, 100, 'Bobby', ['python', '移动互联网', 'UI设计']]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_黄俊英"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "895ae83f",
   "metadata": {},
   "source": [
    "* 虽然列表可以存放不同类型的数据，但是一般情况我们存放的都是同一种类型的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7a280668",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 允许重复的数据**（数据重复性）**\n",
    "网三_python成绩 = [95,96,94,94,97,93]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "20edf087",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 96, 94, 94, 97, 93]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_python成绩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bb4a353",
   "metadata": {},
   "source": [
    "## 1.2创建列表\n",
    "> 1.使用list（）创建列表\n",
    "\n",
    "> 2.直接使用[]创建列表，中间的元素用英文,隔开\n",
    "\n",
    "> 3.创建一个空列表，方法也是上面两种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "02cd8631",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 19, 20]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 1. 使用list（）创建列表\n",
    "my_list1 = list((18,19,20))\n",
    "print(my_list1)\n",
    "print(type(my_list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1c78687f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[75, 80, 74, 76]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 2, 直接使用[]创建列表，中间的元素用英文的 , 隔开\n",
    "my_list2 = [75,80,74,76]\n",
    "print(my_list2)\n",
    "print(type(my_list2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36908b35",
   "metadata": {},
   "source": [
    "* 特殊小案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dca75d42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n']\n"
     ]
    }
   ],
   "source": [
    "my_list3 = list(\"python\")\n",
    "print(my_list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c707fe8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ' ', 'L', 'i', 's', 't']\n"
     ]
    }
   ],
   "source": [
    "my_list4 = list(\"Hello List\")\n",
    "print(my_list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "33030c05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3.创建空列表\n",
    "empty_list = list()\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ebf7fe7",
   "metadata": {},
   "source": [
    "## 1.3 查看列表的长度：len（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "99031b70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "079ace48",
   "metadata": {},
   "source": [
    "## 1.4 列表的取值和切片\n",
    "* index：取值\n",
    "* slice：切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5a170348",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list = list(\"python\")\n",
    "python_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c64cc5eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "89eecee7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "60c41ede",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "060135bd",
   "metadata": {},
   "source": [
    "## 1.5 列表的一般方法\n",
    "* 笔记：数据如何处理是数据诞生之后非常重要的事情\n",
    "* 数据的基本操作流程：\n",
    "> 1.增  \n",
    "> 2.减/删除 remove  \n",
    "> 3.查询 find  \n",
    "> 4.修改 update  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9e4d569d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"黄俊英\",\"李华\",\"李鹏\",\"李小明\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "dc7bb6c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 增加\n",
    "help(name_list.append) #输入完list.后按tab键选择append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e3edf3ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9fc24b3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李华', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e6e5da93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5977274a",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"Bob\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "09a93044",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李华', 'Bob', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6bee558",
   "metadata": {},
   "source": [
    "# 2.删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f9af6335",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop) #pop的index=-1默认移除最后一位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "07711b5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树人'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b19d42c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李华', 'Bob', '李鹏', '李小明']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c9c2f0a",
   "metadata": {},
   "source": [
    "* pop()的第二种使用方法：指定某一位（index）删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f6a03246",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "64f766b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', 'Bob', '李鹏', '李小明']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16a5f4d7",
   "metadata": {},
   "source": [
    "* pop()的第二种使用方法：指定某一位（index）删除\n",
    "* 理论上：只要让我知道你的位置，我就可以pop()掉你！\n",
    "> 1.只要满足某种条件的位置，我们就可以用pop（）删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "63dc7bef",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"Bob\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "412a7d85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李鹏', '李小明', 'Bob']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "e90e3323",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f74bf555",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove(\"李鹏\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "3db6f586",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄俊英', '李小明', 'Bob']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9c8e4ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
